package wrs.hcs.functions;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;

public class Hcs_NewPatient {
	
	public static String lastName;
	private static String firstName;
	private static String middleName;
	public static String pin;
	
	public String createPatient(WebDriver driver, String senior){

		driver.findElement(By.xpath("//a[@href='/admission/home.html']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		PatientName name = new PatientName();
		
		try {
			lastName = name.names(1);
			firstName = name.names(2);
			middleName = name.names(3);
		} catch (Exception e) {
			System.out.println(e);
		}
		//search patient
		driver.findElement(By.xpath("//input[@name='param']")).sendKeys(lastName);
		driver.findElement(By.xpath("//input[@name='action']")).click();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		
		//create new patient
		driver.findElement(By.xpath("//div[@id='left_nav']/ul/li[1]/a")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		//input on fields
		driver.findElement(By.xpath("//input[@id='name.firstName']")).sendKeys(firstName);
		driver.findElement(By.xpath("//input[@id='name.middleName']")).sendKeys(middleName);
		Select suffix = new Select(driver.findElement(By.xpath("//select[@id='suffix.code']")));
		suffix.selectByVisibleText("JR");
		if(senior.equals("senior")){
			driver.findElement(By.xpath("//input[@id='birthDate']")).sendKeys("01/01/1935");
		}else{
			driver.findElement(By.xpath("//input[@id='birthDate']")).sendKeys("01/01/1979");	
		}
		driver.findElement(By.xpath("//input[@id='gender1']")).click();
		Select civil = new Select(driver.findElement(By.xpath("//select[@id='civilStatus.code']")));
		civil.selectByVisibleText("SINGLE");
		driver.findElement(By.xpath("//input[@id='birthPlace']")).sendKeys("Manila");
		driver.findElement(By.xpath("//input[@id='presentAddrNumStreet']")).sendKeys("Blk 51 Lot 3");
		driver.findElement(By.xpath("//input[@id='presentAddrBldg']")).sendKeys("Smart Subdivision");
		driver.findElement(By.xpath("//input[@id='presentAddrProvince']")).sendKeys("Bulacan");
		driver.findElement(By.xpath("//input[@id='presentAddrPostalCode']")).sendKeys("1800");
		Select city = new Select(driver.findElement(By.xpath("//select[@id='presentAddrCitySelect']")));
		city.selectByVisibleText("MUNICIPALITY OF BULACAN");
		Select mobile = new Select(driver.findElement(By.xpath("//select[@id='presentContactSelect']")));
		mobile.selectByVisibleText("MOBILE");
		driver.findElement(By.xpath("//input[@id='presentContact1']")).sendKeys("09991234567");
		driver.findElement(By.xpath("//input[@id='chkFillPermanentAddress']")).click();
		driver.findElement(By.xpath("//input[@id='patientAdditionalDetails.occupation']")).sendKeys("Employed");
		driver.findElement(By.xpath("//input[@id='patientAdditionalDetails.employer']")).sendKeys("SLMC");
		driver.findElement(By.xpath("//input[@name='patientAddresses[2].streetNumber']")).sendKeys("8th Flr Tower 2");
		driver.findElement(By.xpath("//input[@name='patientAddresses[2].buildingName']")).sendKeys("RBC Plaza");
		driver.findElement(By.xpath("//input[@id='employerAddrCityInput']")).sendKeys("Pasig City");
		driver.findElement(By.xpath("//input[@name='patientAddresses[2].province']")).sendKeys("Metro Manila");
		Select country = new Select(driver.findElement(By.xpath("//select[@id='employerAddrCountry']")));
		country.selectByVisibleText("PHILIPPINES");
		driver.findElement(By.xpath("//input[@id='employerAddrPostalCode']")).sendKeys("1234");
		driver.findElement(By.xpath("//input[@id='erLastName']")).sendKeys("Emergency");
		driver.findElement(By.xpath("//input[@id='erFirstName']")).sendKeys("Name");
		
		//button create new patient
		driver.findElement(By.xpath("//input[@value='Create New Admission']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		if(senior.equals("senior")){
			driver.findElement(By.xpath("//html/body/div[5]/div[3]/div/button")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}
		pin = driver.findElement(By.xpath("//div[@id='admissionInfo']/div[2]/div[1]/div")).getText();
		pin = pin.replaceAll("\\s","");
		System.out.println("New Patient Details:");
		System.out.println("Last Name: " +lastName);
		System.out.println("PIN: " +pin);
		String toReturn = lastName + "," + pin;
		return toReturn;
		
	}
	
	
}
